package com.jkc.xh.utils;

import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.app.WallpaperManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.net.Uri;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.inputmethod.InputMethodManager;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

public class Tool {


    public static DisplayMetrics getDisplayMetrics(Context context) {
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        return dm;
    }

    public static int dip2px(Context context, float dip) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dip * scale + 0.5f * (dip >= 0 ? 1 : -1));
    }

    public static int px2dip(Context context, int px) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (px / scale + 0.5f * (px >= 0 ? 1 : -1));
    }


    public static int getStatusBarHeight(Context context) {
        Resources resources = context.getResources();
        int resourceId = resources.getIdentifier("status_bar_height", "dimen",
                "android");
        int height = resources.getDimensionPixelSize(resourceId);
        return height;
    }

    public static int getNavigationBarHeight(Context context) {
        Resources resources = context.getResources();
        int resourceId = resources.getIdentifier("navigation_bar_height",
                "dimen", "android");
        int height = resources.getDimensionPixelSize(resourceId);
        return height;
    }

    @SuppressLint("NewApi")
    public static boolean checkDeviceHasNavigationBar(Context context) {

        // 通过判断设备是否有返回键、菜单键(不是虚拟键,是手机屏幕外的按键)来确定是否有navigation bar
        boolean hasMenuKey = ViewConfiguration.get(context)
                .hasPermanentMenuKey();
        boolean hasBackKey = KeyCharacterMap
                .deviceHasKey(KeyEvent.KEYCODE_BACK);

        if (!hasMenuKey || !hasBackKey) {
            // 做任何你需要做的,这个设备有一个导航栏
            return true;
        }
        return false;
    }


    public static boolean checkEmail(String email) {
        return stringFilter(
                "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$",
                email);
    }

    public static Boolean stringFilter(String regEx, String str)
            throws PatternSyntaxException {
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.matches();
    }

    public static String ToDBC(String input) {
        char[] c = input.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if (c[i] == 12288) {
                c[i] = (char) 32;
                continue;
            }
            if (c[i] > 65280 && c[i] < 65375)
                c[i] = (char) (c[i] - 65248);
        }
        return new String(c);
    }

    public static String stringFilter(String str) {
        str = str.replaceAll("【", "[").replaceAll("】", "]")
                .replaceAll("！", "!").replaceAll("：", ":");// 替换中文标号
        String regEx = "[『』]"; // 清除掉特殊字符
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.replaceAll("").trim();
    }

    public static void hideInputMethod(Context context, View view) {
        try {
            InputMethodManager manager = (InputMethodManager) context
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            manager.hideSoftInputFromWindow(view.getWindowToken(),
                    InputMethodManager.HIDE_NOT_ALWAYS);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static String getAppVersion(Context context, boolean isVersionCode) {
        String resoult = "";
        PackageManager packageManager = context.getPackageManager();
        PackageInfo packInfo = null;
        try {
            packInfo = packageManager.getPackageInfo(context.getPackageName(),
                    0);
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
        resoult = packInfo.versionName;
        if (isVersionCode) {
            resoult = String.valueOf(packInfo.versionCode);
        }
        return resoult;
    }

    public static boolean isSingleScreen(Context context) {
        boolean result = false;
        WallpaperManager wallpaperManager = WallpaperManager
                .getInstance(context);
        int desiredHeight = wallpaperManager.getDesiredMinimumHeight();
        int desiredWidth = wallpaperManager.getDesiredMinimumWidth();
        if ((desiredHeight > 0 && desiredWidth > 0)
                && (desiredHeight > desiredWidth)) {
            result = true;
        }
        return result;
    }


    public static void showKeyboard(Context context, View view) {
        view.requestFocus();
        ((InputMethodManager) context
                .getSystemService(Context.INPUT_METHOD_SERVICE))
                .toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
    }


    public static boolean isTopActivity(Context context) {
        ActivityManager am = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        // android 如何判断程序是否在前台运行
        List<RunningTaskInfo> tasksInfo = am.getRunningTasks(1);
        L.d("getTopActivityPackageName",
                tasksInfo.get(0).topActivity.getPackageName());
        if (tasksInfo.size() > 0) {
            // 应用程序位于堆栈的顶层
            if ("com.bodong.androidwallpaper"
                    .equals(tasksInfo.get(0).topActivity.getPackageName())) {
                return true;
            }
        }
        return false;
    }

    public static void openBrowser(Context context, String url) {
        try {
            if (!TextUtils.isEmpty(url)) {
                Intent intent = new Intent();
                intent.setAction("android.intent.action.VIEW");
                Uri uri = Uri.parse(url);
                intent.setData(uri);
                context.startActivity(intent);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static boolean equals(Object a, Object b) {
        return (a == null) ? (b == null) : a.equals(b);
    }


    public static String trim(String value) {
        return TextUtils.isEmpty(value) ? value : value.trim();
    }


    public static String formatSecondSimpleDate(long second) {
        return formatSimpleDate(second * 1000);
    }

    /**
     * @param timeMillis
     * @return 格式化 yyyy年MM月dd日
     */
    public static String formatSimpleDate(long timeMillis) {
        return new SimpleDateFormat("yyyy年MM月dd日").format(new Date(timeMillis));
    }

    public static String formatSecondSimpleFullDate(long second) {
        return formatSimpleDateFull(second * 1000);
    }

    public static String formatSecondSimpleTimeDate(long second) {
        return formatSimpleDateTime(second * 1000);
    }

    /***
     * @param timeMillis
     * @return 格式化 12:12:12
     */
    public static String formatSimpleDateTime(long timeMillis) {
        return new SimpleDateFormat("hh:mm:ss").format(new Date(timeMillis));
    }

    /***
     * @param timeMillis
     * @return 格式化 12:12:12
     */
    public static String formatSimpleDateFull(long timeMillis) {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(timeMillis));
    }

    /**
     * 格式化价格 2位小数
     *
     * @param price
     * @return
     */
    public static String formatPrice(double price) {
        BigDecimal b = new BigDecimal(price);
        double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        return f1 + "";
    }

    /**
     * 格式化价格 2位小数
     *
     * @param price
     * @return
     */
    public static String formatPrice(String price) {
        String res;
        try {
            BigDecimal b = new BigDecimal(price);
            res = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue() + "";
        } catch (Exception e) {
            e.printStackTrace();
            res = price;
        }
        return res;
    }

    /**
     * 格式化为整数,往上进位, 整数
     *
     * @param num
     * @return
     */
    public static String formatNumNoScale(String num) {
        String res;
        try {
            double number = Double.parseDouble(num);
            res = (int) number + "";
        } catch (Exception e) {
            e.printStackTrace();
            res = num;
        }
        return res;
    }
}
